package team.yidong.fenda.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import team.yidong.fenda.entity.Answer;
import team.yidong.fenda.entity.Master;
import team.yidong.fenda.entity.Question;

import java.util.List;

/**
 * Created by xuanlong on 2017/10/15.
 */
public interface AnswerRepository extends JpaRepository<Answer, Integer> {
    @Query("select answer from Answer answer join answer.question question join answer.master master where question.id=?1 and master.isBlack<>'是'")
    List<Answer> findAnswerByQuestionId(Integer questionId);

    @Query("select answer from Answer answer join answer.master master  where master.openId=?1 ")
    List<Answer> findAnswerByMaster(String openId);

    @Query("select count(answer) from Answer answer join answer.question question join question.wxOrder wxOrder where wxOrder.id=?1 and answer.starNum<>0")
    long countStarAnswerByOrderId(Integer wxOrderId);

    @Query("select distinct question, count(answer.id) AS answer_count FROM Answer answer right join answer.question question where question not in(select  question from Question question join question.answerList answer join answer.master master where master.openId=?1 ) and question.status<>'black' and question.questionClass=?2 and question.answerStatus='可回答' and question.isOneMaster=0 GROUP BY question.id  ORDER BY answer_count ASC,question.creatDate ASC")
    Page<Question> findQuestionOrderByAnswerNum(String openid, String questionClass, Pageable pageable);
}
